package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.server.vo.SoundVo;
import com.tanhua.server.service.SoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Create By Administrator on 2021/7/11.
 */
@RestController
@RequestMapping("peachblossom")
public class SoundController {
    @Autowired
    private SoundService soundService;

    /**
     * 发布音频
     *
     * @param soundFile 音频文件
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveSound(@RequestParam("soundFile") MultipartFile soundFile) {
        try {
            Boolean bool = this.soundService.saveSound(soundFile);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping
    public ResponseEntity<SoundVo> getSound(){
        try {
            SoundVo sound = this.soundService.getSound();
            if (ObjectUtil.isNotEmpty(sound)){
                return ResponseEntity.ok(sound);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
